package com.smile.web.home;

import com.smile.config.entity.User;
import com.smile.config.entity.repository.UserRepository;
import jdk.nashorn.internal.runtime.options.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private UserRepository userRepository;


    @Override
    public List<User> findAllUser() {


        return userRepository.findAll();
    }

    @Override
   // @Transactional
    public User findUserById(Long id) {
        Calendar now = Calendar.getInstance();
        now.set(Calendar.YEAR, 2017);
        Optional<User> userOption = userRepository.findById(id);

       // userOption.get().setRegTime(now.getTime());

        return userOption.get();
    }

    @Override
    public void addUser() {
        User user = new User();
        user.setUserName("xidong");
        user.setEmail("chenxidong@126.com");
        user.setNickName("cxd");
        user.setPassWord("123");
        user.setRegTime(new Date());

        userRepository.save(user);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        Random random = new Random();
        user.setRegTime(new Date());
        user.setPassWord(random.nextInt(100) + "");
        userRepository.save(user);

    }

    @Override
    @Transactional
    public void deleteUser(User user) {

        userRepository.deleteById(user.getId());
    }
}
